﻿module Sustainalytics.CompanyOverview {
    
    export class CompanyOverviewService implements ICompanyOverviewService {

        constructor(private $http: ng.IHttpService, private $q: ng.IQService) {
        }

        public getEsgOverallPerformance(companyId: string, esgProfileId: string): ng.IPromise<EsgOverallPerformanceDto> {
            var ctx = this;

            var url = '/esgratingserv/api/v1/company/' + companyId + '/esgOverallPerformanceSummary/' + '?esgProfileId=' + esgProfileId;

            var promise = ctx.$http.get(url);

            var def = ctx.$q.defer();
            promise.then(
                // success
                res => {
                    def.resolve(res.data);
                },
                // error
                reason => {
                    def.reject(reason);
                });

            return def.promise;
        }

        public getControversyQualitativePerformance(companyId: string): ng.IPromise<ControversyIndicatorDto[]> {
            var ctx = this;


            var url = '/controversiesserv/api/v1/company/' + companyId + '/controversyIndicator/' + companyId + '?qualitativeperformance=true';

            var deferred = ctx.$q.defer();

            var promise = ctx.$http.get(url);

            promise.then(
                (result: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) => {
                    deferred.resolve(result.data);
                },
                (reason: ng.IHttpPromiseCallbackArg<ControversyIndicatorDto[]>) => {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getNewsArticles(companyId: string): ng.IPromise<NewsArticleDto[]> {
            var ctx = this;


            var url = '/controversiesserv/api/v1/company/' + companyId + '/newsArticles';

            var deferred = ctx.$q.defer();


            var promise = ctx.$http.get(url);

            promise.then(
                function (result: ng.IHttpPromiseCallbackArg<NewsArticleDto[]>) {
                    deferred.resolve(result.data);
                },
                function (reason: ng.IHttpPromiseCallbackArg<NewsArticleDto[]>) {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }

        public getEsgResearchType(companyId: string): ng.IPromise<string> {
            var ctx = this;

            var url = "/companyserv/api/v1/companies/" + companyId + "?getResearchType=true";
            var deferred = ctx.$q.defer();

            var promise = ctx.$http.get(url);

            promise.then(
                (result: ng.IHttpPromiseCallbackArg<string>) => {
                    deferred.resolve(result.data);
                },
                (reason: ng.IHttpPromiseCallbackArg<string>) => {
                    deferred.reject(reason);
                });

            return deferred.promise;
        }
    }

    angular.module(companyOverviewModuleName).service('CompanyOverviewService',
        ['$http', '$q', CompanyOverviewService]);
} 